package com.tlxch.server.teacher.impl;

import com.tlxch.dto.TStudentTAlertdto;
import com.tlxch.mapper.*;
import com.tlxch.pojo.*;
import com.tlxch.server.teacher.TAlertreportService;
import com.tlxch.utils.TextSpecialWordChecker;
import com.tlxch.vo.TAlertreportvo;
import com.tlxch.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class TAlertreportServiceImpl implements TAlertreportService {
    @Autowired
    TAlertreportMapper tAlertreportMapper;
    @Autowired
    SAttitudeMapper sAttitudeMapper;
    @Autowired
    SEffectMapper sEffectMapper;
    @Autowired
    SHabitMapper sHabitMapper;
    @Autowired
    SGradesMapper sGradesMapper;
    @Autowired
    TAssessMapper tAssessMapper;
    @Autowired
    TAlertMapper tAlertMapper;
    @Autowired
    WebSocketServer webSocketServer;

    @Transactional
    public TAlertreportvo getAlertReport(TStudentTAlertdto student) {
        TAlertreportvo tAlertreportvo = new TAlertreportvo();
        SAttitude sAttitude = sAttitudeMapper.getByStunameAnd(student);
        SEffect sEffect = sEffectMapper.getByStu(student);
        SHabit sHabit = sHabitMapper.getbystu(student);
        SGrades sGrades = sGradesMapper.getbystu(student);
        TAssess tAssess = tAssessMapper.getstu(student);
        tAlertreportvo.setsAttitude(sAttitude);
        tAlertreportvo.setsEffect(sEffect);
        tAlertreportvo.setsHabit(sHabit);
        tAlertreportvo.setsGrades(sGrades);
        tAlertreportvo.settAssess(tAssess);
        return tAlertreportvo;
    }

    @Transactional
    public void addAlertReport(TAlertreport student) {
        TAlert tAlert = new TAlert();
        tAlert.setTaId(student.getTapTaid());
        System.out.println(student.getTapTaid());
        tAlert.setTaStatus(2);
        tAlertMapper.updateKey(tAlert);
        student.setTapTime(LocalDateTime.now());
        tAlertreportMapper.insertreport(student);
        String message = "{\"msg\":\"" +"您有一条新的综合预警报告，请及时查看！\"}";
        webSocketServer.sendToClient(message,student.getTapSid());
    }
}
